package com.terrynoya.fight.data.parser.sff.compress
{
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	/**
	 * @author terrynoya
	 */
	public class RLE8
	{
		public static function decode(bytes:ByteArray):ByteArray
		{
			var deCompressedLen:int = bytes.readInt();
			var index:int = 0;
			var decoded:ByteArray = new ByteArray();
			decoded.endian = Endian.LITTLE_ENDIAN;
			while(index < deCompressedLen && bytes.bytesAvailable)
			{
				var oneByte:uint = bytes.readByte();
				if((oneByte & 0xC0) == 0x40)
				{
					var color:uint = bytes.readByte();
					for (var i : int = 0; i < (oneByte & 0x3f); i++) 
					{
						decoded.writeByte(color);
						index ++;
					}
				}
				else
				{
					decoded.writeByte(oneByte);
					index ++;
				}
			}
			decoded.position = 0;
			return decoded;
		}
	}
}
